/**
 * Copyright (c) 2023 - present All rights reserved.
 * <p>
 * Reprinted has been protected by the copyright laws.
 */

package cn.myxl.smart.devkit.commit.configuration;

import cn.myxl.smart.devkit.commit.model.SettingState;
import cn.myxl.smart.devkit.core.util.I18nUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.util.NlsContexts;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import cn.myxl.smart.devkit.commit.persistent.HelperSetting;

/**
 * @author zhangyang
 * @date 2023-11-13 11:48
 */

@Slf4j
public class HelperConfiguration implements SearchableConfigurable {
	HelperSetting helperSetting;

	private HelperUI helperUI;

	public HelperConfiguration() {
		helperSetting = ApplicationManager.getApplication().getService(HelperSetting.class);
	}

	@Override
	public @NotNull @NonNls String getId() {
		return "cn.myxl.smart.devkit.commit.configuration.HelperConfiguration";
	}

	@Override
	public @NlsContexts.ConfigurableName String getDisplayName() {
		return I18nUtil.get("displayName");
	}

	@Override
	public @Nullable JComponent createComponent() {
		if (helperUI == null) {
			helperUI = new HelperUI(helperSetting);
		}

		return helperUI.getMainPanel();
	}

	@Override
	public boolean isModified() {
		return this.helperUI != null && this.helperUI.isModified();
	}

	@Override
	public void apply() throws ConfigurationException {
		SettingState settingState = helperUI.apply();

		helperSetting.updateSettingState(settingState);
	}

	@Override
	public void reset() {
		this.helperUI.reset();
	}
}
